package cn.smvc.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.smvc.service.SendMsgToWechatService;
import cn.smvc.wechat.pojo.Token;
import cn.smvc.wechat.test.entity.WeChatAccount;
import cn.smvc.wechat.test.service.WeChatAccountService;
import cn.smvc.wechat.util.CommonUtil;

@Service("sendMsgToWechatService")
public class SendMsgToWechatServiceImpl implements SendMsgToWechatService{

	@Autowired
	private WeChatAccountService weChatAccountService;
	
	@Override
	public Map<String, Object> sendTemplateMsg(Map<String, Object> param) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		String openId = param.get("touser").toString();
		String templateId = param.get("template_id").toString();
		String accountId = param.get("accountId").toString();
		JSONObject json = (JSONObject) param.get("JSONObject");
		
		WeChatAccount account = weChatAccountService.get(Integer.parseInt(accountId));
		Token token = CommonUtil.getToken(accountId, account.getAppId(), account.getAppsecret());
		
		result = CommonUtil.sendTempMsg(templateId, openId, json, token.getAccessToken());
		System.out.println(JSON.toJSONString(result));
		return result;
	}

	
	
	
}
